package com.ruoyi.eam.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.eam.constants.EamConstants;
import com.ruoyi.eam.doman.EamAsset;
import com.ruoyi.eam.doman.EamStockD;
import com.ruoyi.eam.mapper.EamStockDMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eam.mapper.EamStockMapper;
import com.ruoyi.eam.doman.EamStock;
import com.ruoyi.eam.service.IEamStockService;

/**
 * 入库信息Service业务层处理
 *
 * @author xl
 * @date 2024-06-27
 */
@Service
public class EamStockServiceImpl implements IEamStockService
{
    @Autowired
    private EamStockMapper eamStockMapper;
    @Autowired
    private EamStockDMapper stockDMapper;


    /**
     * 查询入库信息
     *
     * @param stockId 入库信息主键
     * @return 入库信息
     */
    @Override
    public EamStock selectEamStockByStockId(Long stockId)
    {
        return eamStockMapper.selectEamStockByStockId(stockId);
    }

    /**
     * 查询入库信息列表
     *
     * @param eamStock 入库信息
     * @return 入库信息
     */
    @Override
    public List<EamStock> selectEamStockList(EamStock eamStock)
    {
        return eamStockMapper.selectEamStockList(eamStock);
    }

    /**
     * 新增入库信息
     *
     * @param eamStock 入库信息
     * @return 结果
     */
    @Override
    public int insertEamStock(EamStock eamStock)
    {
        //入库业务处理
        eamStock.setCreateTime(DateUtils.getNowDate());
        //入库编码规则
        //获取当前时间
        String nowDate = DateUtils.getDateYYYYMMDD();
        //获取最后一条入库编码
        String stockCodeLast = eamStockMapper.queryEamStockList().get(eamStockMapper.queryEamStockList().size() - 1).getStockcode();
        System.out.println("stockCodeLast"+stockCodeLast);
        //截取流水线
        String serial =  stockCodeLast.substring(10,15);
        if(serial.equals("99999")){
            serial = "00001";
        }else{
            int num = Integer.parseInt(serial) + 1;
            serial = String.format("%05d",num);
        }
        //最终编码
        String stockCode = EamConstants.STOCK_CODE_PR + nowDate + serial;
        eamStock.setStockcode(stockCode);
        eamStock.setAuditType(3L);  //待审核
        eamStock.setStockTime(DateUtils.getNowDate());
        eamStock.setStockType(1L);  //登记入库
        eamStock.setIsDel(1L);
        if(eamStock.getAssetId() != null){
            List<EamStockD> list = stockDMapper.selectEamStockDByAssetId(eamStock.getAssetId());
            if(list.size() == 0){
                return eamStockMapper.insertEamStock(eamStock);
            }else{
                //已有入库数据
                return 0;
            }
        }else{
            System.out.println("H4");
            return eamStockMapper.insertEamStock(eamStock);
        }
    }

    /**
     * 修改入库信息
     *
     * @param eamStock 入库信息
     * @return 结果
     */
    @Override
    public int updateEamStock(EamStock eamStock)
    {
        return eamStockMapper.updateEamStock(eamStock);
    }

    /**
     * 批量删除入库信息
     *
     * @param stockIds 需要删除的入库信息主键
     * @return 结果
     */
    @Override
    public int deleteEamStockByStockIds(Long[] stockIds)
    {
        return eamStockMapper.deleteEamStockByStockIds(stockIds);
    }

    /**
     * 删除入库信息信息
     *
     * @param stockId 入库信息主键
     * @return 结果
     */
    @Override
    public int deleteEamStockByStockId(Long stockId)
    {
        return eamStockMapper.deleteEamStockByStockId(stockId);
    }
}
